package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.border.TitledBorder;
import javax.swing.JComboBox;

import common.Glob;

import Controller.Client;
import Entity.ParkingLot;
import Entity.ParkingSpot;
import Entity.ParkingSpot.pSpot;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;

//import com.itextpdf.text.Document;
//import com.itextpdf.text.pdf.PdfContentByte;
//import com.itextpdf.text.pdf.PdfTemplate;
//import com.itextpdf.text.pdf.PdfWriter;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileOutputStream;

/**
 * this is the GUI for viewing the statuc picture report to the company manager. he will chose the parking lot and show the state. it will also make him PDF file
 * @author Sandra Krasko and Moran Abilea
 *
 */
public class StatusPictureWin extends JPanel {
	int cols=0;
	private JPanel contentPane;
	
	JFrame frame;
	JPanel lastPanel;
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}

	/**
	 * is the constructor.it has a combo box to chose from and a save buton for creating the PDF.
	 */
	public StatusPictureWin() {

		setBounds(100, 100, 918, 403);
		
		final JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setBounds(422, 106, 38, 20);
		for(int i=1; i<=3; i++) comboBox_1.addItem(Integer.toString(i));
		
		JLabel lblStatusPicture = new JLabel("Status Picture");
		lblStatusPicture.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblStatusPicture.setBounds(168, 11, 97, 14);
		
		final JCheckBox[] box=new JCheckBox[24];
		int x=39,y=39;
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<8;j++)
			{
				box[j*3+i]=new JCheckBox();
				box[j*3+i].setBounds(x+j*25, y, 21, 23);
				contentPane.add(box[j*3+i]);
				box[j*3+i].setVisible(false);
			}
			y+=25;
		}
		
		final JComboBox comboBox = new JComboBox();
		comboBox.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				
			}
		});
		comboBox.setBounds(422, 76, 38, 20);
			for(int i=1; i<=6; i++) comboBox.addItem(Integer.toString(i));
		
		
		JLabel lblParkingLotNumber = new JLabel("Parking Lot Number");
		lblParkingLotNumber.setBounds(299, 79, 125, 14);
		
		JLabel lblFloor = new JLabel("Floor");
		lblFloor.setBounds(384, 109, 40, 14);
		
		JButton btnView = new JButton("View Floor");
		btnView.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cols=Client.getParkingLots_sorted()[Integer.valueOf((String) comboBox.getSelectedItem())-1].getCols();
				ParkingSpot[][][] ps=Client.getParkingLots_sorted()[Integer.valueOf((String) comboBox.getSelectedItem())-1].getParking_spots();
				for (int i=0; i<3; i++)
						for (int j=0; j<cols; j++)
						{
							if (ps[i][Integer.valueOf((String) comboBox_1.getSelectedItem())-1][j].getStatus()==pSpot.FREE)
								box[j*3+i].setSelected(false);
							else
								box[j*3+i].setSelected(true);
						}
				switch (cols)
				{
				
				case 4:	
					for(int i=0;i<3;i++)
					{
						for(int j=0;j<4;j++)
						{
							box[j*3+i].setVisible(true);
						}
						for(int j=4;j<8;j++)
						{
							box[j*3+i].setVisible(false);
						}
						
					} 
					break;
				case 5:
					for(int i=0;i<3;i++)
					{
						for(int j=0;j<5;j++)
							{
								box[j*3+i].setVisible(true);
							}
						for(int j=5;j<8;j++)
						{
							box[j*3+i].setVisible(false);
						}
					}
					break;
				case 6:
					for(int i=0;i<3;i++)
					{
						for(int j=0;j<6;j++)
							{
								box[j*3+i].setVisible(true);
							}
						for(int j=6;j<8;j++)
						{
							box[j*3+i].setVisible(false);
						}
					}
					break;
				case 7:
					for(int i=0;i<3;i++)
					{
						for(int j=0;j<7;j++)
							{
								box[j*3+i].setVisible(true);
							}
						for(int j=7;j<8;j++)
						{
							box[j*3+i].setVisible(false);
						}
					}
					break;
				case 8:
					for(int i=0;i<3;i++)
					{
						for(int j=0;j<8;j++)
							{
								box[j*3+i].setVisible(true);
							}
					}
					break;
				}
			}
			
		});
		btnView.setBounds(340, 137, 102, 23);
		
		JButton btnNewButton = new JButton("Save to PDF and open it");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {/*
				Document document = new Document();
				try {
					String myworkspace;
					myworkspace=JOptionPane.showInputDialog("Please enter a path for saving the PDF file.\n for example: C:\\Users\\mabos555\\workspace");
				    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(myworkspace+"\\mytest.pdf"));//C:\\Users\\mabos555\\workspace
				    document.open();
				    PdfContentByte contentByte = writer.getDirectContent();
				    PdfTemplate template = contentByte.createTemplate(500, 500);
				    Graphics2D g2 = template.createGraphics(500, 500);
				    this.print(g2);
				    g2.dispose();
				    contentByte.addTemplate(template, 30, 300);
				    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+myworkspace+"\\mytest.pdf");//C:\\Users\\mabos555\\workspace
				} catch (Exception e) {
				    //e.printStackTrace();
					JOptionPane.showMessageDialog(null, e);
				}
				finally{
				    if(document.isOpen()){
				        document.close();
				    }
				}*/
			}
		});
		btnNewButton.setBounds(280, 171, 162, 23);
		
		final JCheckBox chckbxNewCheckBox = new JCheckBox("");
		chckbxNewCheckBox.setBounds(44, 54, 21, 23);
		chckbxNewCheckBox.setVisible(false);
	}
}